using SS;
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace TVSGPP.bookGPP
{
	internal class clsProductExt
	{
		public void syncProductExt()
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = "DELETE FROM TabProductsExt WHERE PID NOT IN (SELECT PID FROM TabProducts)";
			sqlCommand.ExecuteNonQuery();
			sqlCommand.CommandText = "INSERT INTO TabProductsExt(PID,BarCode,GpID,P_Name,ManID,P_DonViTinh,P_PriceBase,P_PriceRetail,P_PriceTrade,P_Notes,currBalance,MinBalance,MaxBalance,KipLight,KipWarm,KipChildren,KipSaveCondition,NameSearch,possID,possName,DayLive,DayNumberWarning,NumberWarning,InfoInclude,Usage,SellWithBill,FocusMind,interaction) SELECT * FROM TabProducts WHERE PID NOT IN (SELECT PID FROM TabProductsExt)";
			sqlCommand.ExecuteNonQuery();
			sqlConnection.Close();
			sqlConnection.Dispose();
			MessageBox.Show("Quá trình đồng bộ đã hoàn thành !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}
		public string getAgentName(string ID)
		{
			string result = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = "SELECT Name FROM TabAgent WHERE ID = '" + ID + "'";
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				result = sqlDataReader["Name"].ToString();
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public void syncProductExt(DataGridView dgv)
		{
			try
			{
				double num = 0.0;
				double num2 = 0.0;
				double num3 = 0.0;
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				for (int i = 0; i < dgv.RowCount; i++)
				{
					try
					{
						num = double.Parse(dgv.Rows[i].Cells["RootPrice"].Value.ToString());
					}
					catch
					{
						num = 0.0;
					}
					try
					{
						num2 = double.Parse(dgv.Rows[i].Cells["Discount"].Value.ToString());
					}
					catch
					{
						num2 = 0.0;
					}
					try
					{
						num3 = num * (1.0 - num2 / 100.0);
					}
					catch
					{
						num3 = 0.0;
					}
					if (dgv.Rows[i].Cells["GpID"].Value.ToString().Trim() == "")
					{
						sqlCommand.CommandText = string.Concat(new string[]
						{
							"UPDATE TabProductsExt SET Agent1 = ",
							dgv.Rows[i].Cells["Agent1"].Value.ToString(),
							", Agent2 = ",
							dgv.Rows[i].Cells["Agent2"].Value.ToString(),
							", Agent3 = ",
							dgv.Rows[i].Cells["Agent3"].Value.ToString(),
							", RootPrice = ",
							dgv.Rows[i].Cells["RootPrice"].Value.ToString(),
							", Discount = ",
							dgv.Rows[i].Cells["Discount"].Value.ToString(),
							", DiscountPrice = ",
							num3.ToString(),
							" WHERE [Pid] = ",
							dgv.Rows[i].Cells["dg6"].Value.ToString()
						});
						sqlCommand.ExecuteNonQuery();
					}
				}
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
	}
}
